// pages/home/home.js
//var base = require("../../utils/base.js"); //require引入
const app = getApp();
Page({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    dormitoryname:'你的宿舍',
    myname:'你的昵称',
    myimg:"../../images/home/me.jpg",
    imgSrc: "../../images/home/we.jpg",
    total:1,
    routers: [
      {
        name: '靓仔休息室',
        url: '/pages/setting/setting',
        icon: '../../images/setting.png',
        code: '10'
      },
      {
        name: '小黑板',
        url: '/pages/announcement_list/announcement_list',
        icon: '../../images/announcement.png',
        code: '11'
      },
      {
        name: '公主日记',
        url: '/pages/dontforget/dontforget',
        icon: '../../images/dontforget.png',
        code: '12'
      },
      {
        name: '来抽奖吧',
        url: '/pages/chouqian/chouqian',
        icon: '../../images/chouqian.png',
        code: '13'
      },
      {
        name: '倒计时',
        url: '/pages/clock/clock',
        icon: '../../images/tomato.png',
        code: '14'
      },
      {
        name: '公共物品',
        url: '/pages/items_list/items_list',
        icon: '../../images/items.png',
        code: '15'
      },
      {
        name:'今天吃什么',
        url: '/pages/food/food',
        icon: '../../images/food.png',
        code: '16'
      },
      {
        name: '打赏',
        url: '/pages/money/money',
        icon: '../../images/money.png',
        code: '17'
      },
      {
        name: '更多',
        url: '/pages/more/more',
        icon: '../../images/more.png',
        code: '18'
      }
    ],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
  },  
  tovsetting(){
    wx.navigateTo({
      url: '/pages/setting/setting',
    })
  },
  tov(){
    wx.switchTab({
      url: '/pages/myself/myself',
    })
  },
  getavatarandname() {
    const db = wx.cloud.database();
    const dormitoryCollection = db.collection('dormitory');
    const userInfoCollection = db.collection('userInfo');
     // 获取小程序实例
    const dormitoryId = app.globalData.dormitoryId; // 从小程序实例的全局数据中获取dormitoryId
    console.log("sushe"+dormitoryId);
    dormitoryCollection.where({
      dormitoryId: dormitoryId
    }).field({
      member: true,
      name: true,
      avatar: true
    }).get().then(dormRes => {
      const dormData = dormRes.data[0];
      const members = dormData.member;
      const totalMembers = members.length;
  
      this.setData({
        dormitoryname: dormData.name,
        total: totalMembers,
        imgSrc:dormData.avatar,
      });
  
      const openidList = members.map(member => member.openid);
      userInfoCollection.where({
        openid: db.command.in(openidList)
      }).get().then(userInfoRes => {
        const userInfoData = userInfoRes.data;
        const currentUserInfo = members.find(member => member.openid === app.globalData.openid);
  
        if (currentUserInfo) {
          this.setData({
            myname: currentUserInfo.name
          });
    
          const currentUserInfoFromUserInfo = userInfoData.find(info => info.openid === app.globalData.openid);
          if (currentUserInfoFromUserInfo && currentUserInfoFromUserInfo.avatar) {
            this.setData({
              myimg: currentUserInfoFromUserInfo.avatar
            });
            console.log(this.data.myimg)
          }
        }
      }).catch(err => {
        console.error('获取用户信息失败', err);
      });
    }).catch(err => {
      console.error('获取宿舍信息失败', err);
    });
  },  
  //加载内容
  onLoad: function () {   
      this.getavatarandname();
  },
  onshow(){
    this.getavatarandname();
  },
  /**
   * 组件的方法列表
   */


  //点击页面跳转
  onProductsItmesTap:function(event){
    var id = event.currentTarget.dataset.id;
    console.log("even:" + id);
      wx.navigateTo({
        url: 'homedetails/homedetails?id='+id,
      });
  },
  onPullDownRefresh(){
    this.getavatarandname();
  }
 


})
